package com.itheima.controller;


import com.itheima.entity.Emp;
import com.itheima.entity.EmpQueryParam;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.time.LocalDate;


@Slf4j
@RestController
public class EmpController {

    private final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;
    @Autowired
    private InternalResourceViewResolver defaultViewResolver;


//    /**
//     * 分页查询
//     * @param page
//     * @param pageSize
//     * @return
//     */
//    @GetMapping("/emps")
//    public Result Page(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10")Integer pageSize){
//        log.info("分页查询：{}{}",page,pageSize);
//        PageBean pageBean = empService.page(page,pageSize);
//        return Result.success(pageBean);
//    }

//    @GetMapping("/emps")
//    public Result Page(
//            String name,
//            Integer gender,
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
//            @RequestParam(defaultValue = "1") Integer page,
//            @RequestParam(defaultValue = "10") Integer pageSize) {
//        log.info("分页查询：{}{}{}{}{}{}", name, gender, begin, end, page, pageSize);
//        PageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);
//        return Result.success(pageBean);
//    }

    @GetMapping("/emps")
    public Result Page(EmpQueryParam param) {
        log.info("分页查询：{}{}{}{}{}{}", param.getName(),param.getGender(),param.getBegin(),param.getEnd(),param.getPage(),param.getPageSize());
        PageBean pageBean = empService.page(param);
        return Result.success(pageBean);
    }

    /**
     * 新增员工
     * @param emp
     * @return
     */
    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp){
        log.info("新增员工：{}",emp);
        empService.save(emp);
        log.info(emp.getExprList().toString());
        return Result.success();
    }

}
